package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    @PostMapping
    public R<String> addCategory(@RequestBody Category category) {


        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();

        qw.eq(Category::getName,category.getName());

        int count = categoryService.count(qw);
        if (count > 0) {
            throw new CustomException("该菜品分类已存在");
        }

        boolean save = categoryService.save(category);

        if (save) {
            return R.success("新增菜品成功..");
        }
        return R.error("新增菜品失败!");
    }

    @GetMapping("/page")
    public R<Page> paginationQueryMethod(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10")Integer pageSize) {

        Page<Category> catePage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();

        qw.orderByAsc(Category::getSort);

        Page<Category> categoryPage = categoryService.page(catePage, qw);

        return R.success(categoryPage);
    }


    /**
     * delete删除操作
     */
    @DeleteMapping
    public R<String> deleteClassification(BigInteger ids) {
        //当菜品下有该分类id存在时不能删除
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        dqw.eq(Dish::getCategoryId,ids);
        List<Dish> dish = dishService.list(dqw);
        if (null != dish && dish.size() > 0) {
            throw new CustomException("该分类下仍含有菜品，删除失败%%");
        }

        //当套餐下有该分类id存在时也不能删除
        LambdaQueryWrapper<Setmeal> sqw = new LambdaQueryWrapper<>();
        sqw.eq(Setmeal::getCategoryId,ids);
        int count = setmealService.count(sqw);
        if (count > 0) {
            throw new CustomException("该分类下仍含有套餐，删除失败%%");
        }

        boolean result = categoryService.removeById(ids);
        if (result) {
            return R.success("删除分类成功!");
        } else {
            return R.error("删除分类失败!");
        }
    }

    /**
     * 修改操作
     */
    @PutMapping
    public R<String> modifyClassInformation(@RequestBody Category category) {
        log.info("传入的分类:{}",category);
        //修改之前查询该修改info的分类名称是否与其它数据雷同
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getName,category.getName());
        //添加排己条件
        qw.ne(Category::getId,category.getId());
        int count = categoryService.count(qw);
        if (count > 0) {
            throw new CustomException("修改的该分类名已经存在!!");
        }
        //程序执行到这里说明其它数据没有占用该分类名 表示可以进行修改
        boolean updRes = categoryService.updateById(category);
        if (updRes) {
            return R.success("成功!");
        }
        return R.error("失败..");
    }


    /**
     * 菜品分类数据列表查询
     */
    @GetMapping("/list")
    public R<List<Category>> findCategorizationOfDishes(Integer type) {
        log.info("list方法执行了....");

        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        if (type == null ) {
            List<Integer> ints = new LinkedList<>();
            List<Category> categories = categoryService.list();
            for (Category c : categories) {
                Integer cType = c.getType();
                if (ints.indexOf(cType) == -1) {
                    ints.add(cType);
                }
            }
            qw.in(Category::getType, ints);
        }

        qw.eq(null != type,Category::getType,type);

        qw.orderByAsc(Category::getSort);
        qw.orderByDesc(Category::getUpdateTime);
        List<Category> categoryList = categoryService.list(qw);

        if (null != categoryList && 0 != categoryList.size()) {
            return R.success(categoryList);
        }

        return R.error("查询到没有相关数据...");
    }
}
